import { Controller } from "../../mvc/core/Controller";
import { EGuildwarState } from "../view/guildWar/defin/EGuildwarState";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { SGuildWarSingUpTime } from "../../../Message/Public/SGuildWarSingUpTime";
import { GuildwarEnterWindow } from "../view/guildWar/GuildwarEnterWindow";
import { GuildWarIconView } from "../view/guildWar/GuildWarIconView";
import { SGuildWarEnter } from "../../../Message/Public/SGuildWarEnter";
import { IGuildWarOpenModule } from "../../../modules/interfaces/IGuildWarOpenModule";
import { GuildWarOpenModule } from "../../../modules/GuildWarOpenModule";
import { IGuildwarStatusModule } from "../../../modules/interfaces/IGuildwarStatusModule";
import { GuildWarStatusModule } from "../../../modules/GuildWarStatusModule";
import { Alert } from "../../../com/mui/controls/Alert";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { Language } from "../../../extend/language/Language";
import { SecTimer } from "../../../com/gengine/core/frame/SecTimer";
import { ClockManager } from "../manager/ClockManager";
import { TimerType } from "../../../com/gengine/core/frame/TimerType";
import { SGuildWarOpen } from "../../../Message/Public/SGuildWarOpen";
import { GuildWarScoreView } from "../view/guildWar/GuildWarScoreView";
import { SGuildWarSingUpInfo } from "../../../Message/Public/SGuildWarSingUpInfo";
import { SGuildWarNextRing } from "../../../Message/Public/SGuildWarNextRing";
import { TimerAlertWin } from "../view/common/alertwins/TimerAlertWin";
import { SGuildWarLeftNum } from "../../../Message/Public/SGuildWarLeftNum";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { MsgManager } from "../manager/MsgManager";
import { LevelLimitDefin } from "../../common/defin/LevelLimitDefin";
import { RoleAvatar } from "../view/avatar/RoleAvatar";
import { MapFileUtil } from "../scene/map/MapFileUtil";
import { SystemSetter } from "../view/system/SystemSetter";
import { GuildwarRule } from "../view/guildWar/GuildwarRule";
import { GuildWarFlyScene } from "../view/guildWar/GuildWarFlyScene";
type int = number;
//class GuildWarController
    
    export  class GuildWarController extends Controller
    {
        constructor()
        {
            
            super();this._currentState = EGuildwarState.Close;
            return;
        }

        /* internal  */onGuildWarIsNotice(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            GameProxy.guildWar.setGuildWarNotice(loc1);
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.GuildwarSignUpTime, this.onGuildwarSignUpCommand);
            NetDispatcher.addCmdListener(ServerCommand.GuildwarEnter, this.onGuildwarEnterCommand);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarOpen, this.onGuildWarOpenCommand);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarScoreUpdate, this.onGuildWarScoreCommand);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarScoreListUpdate, this.onGuildWarScoreListCommand);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarClose, this.onGuildWarCloseCommand);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarCopyInfosUpdate, this.onCopyInfosUpdate);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarInfoNowUpdate, this.onGuildWarInfoNowUpdate);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarSignUpInfoUpdate, this.onGuildWarSignUpInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarPlayerRewardUpdate, this.onGuildWarPlayerRewardUpdate);
            NetDispatcher.addCmdListener(ServerCommand.GuildUpdateInfo, this.onGuildUpdateInfo);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarNextRing, this.onGuildwarNextRing);
            NetDispatcher.addCmdListener(ServerCommand.GuildWarLeftNumUpdate, this.onGuildWarLeftNumUpdate);
            NetDispatcher.addCmdListener(ServerCommand.GuildUpdateInfo, this.onGuildUpdateInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.ActiveAddGuildWar, this.onActiveAddGuildWarHandler);
            Dispatcher.addEventListener(EventName.GuildwarSignUp, this.onGuildwarSignUp);
            Dispatcher.addEventListener(EventName.GuildWarShowOpenModule, this.onShowOpenModule);
            Dispatcher.addEventListener(EventName.GuildWarEnter, this.onGuildWarEnter);
            Dispatcher.addEventListener(EventName.GuildWarEnterClick, this.onGuildWarEnterClickHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            Dispatcher.addEventListener(EventName.GuildWarGetScoreList, this.onGuildWarGetScoreList);
            Dispatcher.addEventListener(EventName.GuildWarLeave, this.onGuildWarLeave);
            Dispatcher.addEventListener(EventName.GuildWarOpenOtherMap, this.onGuildWarOpenOtherMap);
            Dispatcher.addEventListener(EventName.GuildWarGetTopList, this.onGuildWarGetTopList);
            Dispatcher.addEventListener(EventName.GuildWarShowOrHideStatusModule, this.onShowOrHideStatusModule);
            Dispatcher.addEventListener(EventName.GuildWarIsNotice, this.onGuildWarIsNotice);
            return;
        }

        /* internal  */onGuildwarSignUpCommand(arg1: SGuildWarSingUpTime): void
        {
            this._currentState = EGuildwarState.SignUp;
            GuildwarEnterWindow.instance.show();
            if (cache.guildWar.guildWarSignUpInfo) 
            {
                GuildwarEnterWindow.instance.updateSignUpInfo(cache.guildWar.guildWarSignUpInfo);
            }
            GuildwarEnterWindow.instance.updateGuildWarState(EGuildwarState.SignUp);
            GuildwarEnterWindow.instance.updateEndTime(arg1.endDt);
            GuildWarIconView.instance.show();
            GuildWarIconView.instance.updateState(EGuildwarState.SignUp, arg1.endDt);
            return;
        }

        /* internal  */onGuildWarEnterClickHandler(arg1: DataEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarEnter));
            return;
        }

        /* internal  */onGuildwarEnterCommand(arg1: SGuildWarEnter): void
        {
            if (this._currentState == EGuildwarState.Open) 
            {
                return;
            }
            this._currentState = EGuildwarState.Enter;
            GuildWarIconView.instance.show();
            GuildwarEnterWindow.instance.show();
            GuildwarEnterWindow.instance.updateGuildWarState(EGuildwarState.Enter);
            GuildwarEnterWindow.instance.updateEndTime(arg1.endDt);
            GuildWarIconView.instance.updateState(EGuildwarState.Enter, arg1.endDt);
            return;
        }

        /* internal  */get guildWarOpenModule(): IGuildWarOpenModule
        {
            if (!this._guildWarOpenModule) 
            {
                this._guildWarOpenModule = new GuildWarOpenModule();
            }
            return this._guildWarOpenModule;
        }

        /* internal  */get guildWarStatusModule(): IGuildwarStatusModule
        {
            if (!this._guildWarStatusModule) 
            {
                this._guildWarStatusModule = new GuildWarStatusModule();
            }
            return this._guildWarStatusModule;
        }

        /* internal  */showAlertComein(): void
        {
            var repeatCount: number;
            var onClose: Function;

            var loc1;
            onClose = null;
            onClose = (arg1: int): void
            {
                clear();
                if (arg1 == Alert.OK) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarEnter));
                }
                return;
            }
            if (GameMapUtil.isGuildWar()) 
            {
                return;
            }
            Alert.buttonWidth = 70;
            Alert.okLabel = Language.getString(41844);
            Alert.calcelLabel = Language.getString(41845);
            this._enterSp = Alert.show(Language.getString(41846), null, Alert.OK | Alert.CANCEL, null, onClose);
            if (!this._enterTime) 
            {
                this._enterTime = new SecTimer();
            }
            this._enterTime.reset();
            repeatCount = (cache.guildWar.guildwarEnter.endDt.time - ClockManager.instance.nowDate.time) / 1000 - 10;
            if (repeatCount > 0) 
            {
                this._enterTime.repeatCount = (cache.guildWar.guildwarEnter.endDt.time - ClockManager.instance.nowDate.time) / 1000 - 10;
                this._enterTime.start();
                this._enterTime.addListener(TimerType.COMPLETE, this.onTimerComplete);
            }
            return;
        }

        /* internal  */clear(): void
        {
            this._enterTime.stop();
            return;
        }

        /* internal  */onTimerComplete(arg1: SecTimer): void
        {
            if (this._enterSp && this._enterSp.parent) 
            {
                this._enterSp.parent.removeChild(this._enterSp);
            }
            GameProxy.guildWar.enterGuildWar(0);
            this.guildWarOpenModule.hide();
            return;
        }

        /* internal  */onGuildWarOpenCommand(arg1: SGuildWarOpen): void
        {
            this._currentState = EGuildwarState.Open;
            GuildwarEnterWindow.instance.updateGuildWarState(EGuildwarState.Open);
            if (!GameMapUtil.isGuildWar()) 
            {
                GuildWarIconView.instance.show();
                GuildWarIconView.instance.updateState(EGuildwarState.Open, arg1.endDt);
            }
            return;
        }

        /* internal  */onGuildWarScoreCommand(arg1: Object): void
        {
            GuildWarScoreView.instance.updateScore(cache.guildWar.guildWarScore);
            return;
        }

        /* internal  */onGuildWarScoreListCommand(arg1: Object): void
        {
            this.guildWarStatusModule.updateGuildData(cache.guildWar.guildScoreList);
            this.guildWarStatusModule.updatePlayerData(cache.guildWar.playerScoreList);
            return;
        }

        /* internal  */onGuildWarCloseCommand(arg1: Object): void
        {
            GuildWarIconView.instance.hide();
            if (GuildwarEnterWindow.hasInstance()) 
            {
                GuildwarEnterWindow.instance.hide();
                GuildwarEnterWindow.instance.reset();
            }
            this._currentState = EGuildwarState.Close;
            return;
        }

        /* internal  */onCopyInfosUpdate(arg1: Object): void
        {
            return;
        }

        /* internal  */onGuildWarInfoNowUpdate(arg1: Object): void
        {
            this.updateGuildwarRingLeftTime();
            return;
        }

        /* internal  */onGuildWarSignUpInfoUpdate(arg1: SGuildWarSingUpInfo): void
        {
            GuildwarEnterWindow.instance.updateSignUpInfo(arg1);
            return;
        }

        /* internal  */onGuildWarPlayerRewardUpdate(arg1: Object): void
        {
            var loc2=0;
            var loc1=cache.guildWar.guildWarPlayerReward;
            Alert.timerOut = 15;
            if (loc1.isFail) 
            {
                loc2 = 41854;
            }
            else 
            {
                loc2 = 41800;
            }
            Alert.show(Language.getStringByParam(loc2, loc1.experience.toString(), loc1.contribution.toString(), loc1.guildMoney.toString()));
            return;
        }

        /* internal  */onGuildUpdateInfo(arg1: Object): void
        {
            if (GuildwarEnterWindow.hasInstance()) 
            {
                GuildwarEnterWindow.instance.resetbtnState();
            }
            return;
        }

        /* internal  */onGuildwarNextRing(arg1: SGuildWarNextRing): void
        {
            var nextRing: SGuildWarNextRing;
            var onCloseAlert: Function;

            var loc1;
            onCloseAlert = null;
            nextRing = arg1;
            onCloseAlert = (arg1: int): void
            {
                if (arg1 == Alert.OK) 
                {
                    GameProxy.guildWar.enterGuildWar();
                }
                return;
            }
            if (nextRing.nextRing == 1) 
            {
                return;
            }
            if (nextRing.canEnter) 
            {
                Alert.mode = Alert.Mode_NoneNotModal;
                Alert.alertWinRenderer = TimerAlertWin;
                Alert.buttonWidth = 70;
                Alert.timerOut = 15;
                Alert.okLabel = Language.getString(41847);
                Alert.show(Language.getString(41848), null, Alert.OK, null, onCloseAlert);
            }
            else 
            {
                Alert.buttonWidth = 70;
                Alert.show(Language.getString(41850), null, Alert.OK, null);
            }
            return;
        }

        /* internal  */onGuildWarLeftNumUpdate(arg1: SGuildWarLeftNum): void
        {
            if (GameMapUtil.isGuildWar()) 
            {
                GuildWarScoreView.instance.updateEnemyGuilds(arg1);
            }
            return;
        }

        /* internal  */onGuildUpdateInfoUpdate(arg1: Object): void
        {
            var loc1=0;
            if (GuildwarEnterWindow.hasInstance()) 
            {
                loc1 = 0;
                if (cache.guild.myGuildInfo) 
                {
                    loc1 = cache.guild.myGuildInfo.contribution;
                }
                GuildwarEnterWindow.instance.updateContribution(loc1);
            }
            return;
        }

        /* internal  */onActiveAddGuildWarHandler(arg1: SActiveToClient): void
        {
            if (!GuildWarIconView.instance.isHide) 
            {
                GuildWarIconView.instance.updateActiveToClientInfo(arg1);
            }
            return;
        }

        /* internal  */onGuildwarSignUp(arg1: DataEvent): void
        {
            GameProxy.guildWar.signUpGuildWar();
            return;
        }

        /* internal  */onGuildWarEnter(arg1: DataEvent): void
        {
            var e: DataEvent;
            var guildWarId: int;
            var callBack: Function;

            var loc1;
            guildWarId = 0;
            callBack = null;
            e = arg1;
            callBack = (arg1: int): void
            {
                if (arg1 == Alert.OK) 
                {
                    GameProxy.guildWar.enterGuildWar(guildWarId);
                    guildWarOpenModule.hide();
                }
                return;
            }
            guildWarId = 0;
            if (e.data) 
            {
                guildWarId = int(e.data);
            }
            if (!cache.guild.myGuildInfo) 
            {
                MsgManager.showRollTipsMsg(Language.getString(41802));
                return;
            }
            if (cache.role.roleInfo.level < 30) 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(41803, LevelLimitDefin.GuildWarLimitLevel.toString()));
                return;
            }
            if (GameMapUtil.isGuildWar()) 
            {
                GameProxy.guildWar.enterGuildWar(guildWarId);
                this.guildWarOpenModule.hide();
            }
            else 
            {
                RoleAvatar.alertChangeMode(callBack);
            }
            return;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            if (GameMapUtil.isGuildWar(MapFileUtil.mapID)) 
            {
                if (this._guildWarStatusModule) 
                {
                    this._guildWarStatusModule.isShowLeave(true);
                }
                if (!this._isInGuildWarMap && SystemSetter.currentSetter.isTipGuildwarRule) 
                {
                    GuildwarRule.instance.show();
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyEnterCopyMap));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, false));
                this._isInGuildWarMap = true;
                GuildWarIconView.instance.hide();
                GuildWarScoreView.instance.show();
                if (GuildwarEnterWindow.hasInstance()) 
                {
                    GuildwarEnterWindow.instance.hide();
                }
                this.updateGuildwarRingLeftTime();
                if (cache.guildWar.guildWarInfoNow) 
                {
                    GuildWarScoreView.instance.updateEnemyGuilds(cache.guildWar.guildWarLeftNum);
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            }
            else 
            {
                if (!this._isInGuildWarMap) 
                {
                    return;
                }
                GuildwarRule.instance.hide();
                this._isInGuildWarMap = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeaveCopyMap));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, true));
                GuildWarScoreView.instance.hide();
                GuildWarFlyScene.instance.hide();
                if (this._guildWarStatusModule) 
                {
                    this._guildWarStatusModule.isShowLeave(false);
                }
                if (this._currentState == EGuildwarState.Close) 
                {
                    GuildWarIconView.instance.hide();
                }
                else 
                {
                    GuildWarIconView.instance.show();
                }
            }
            return;
        }

        /* internal  */showGuildWarIconView(): void
        {
            GuildWarIconView.instance.show();
            if (cache.guild.myGuildInfo && cache.role.roleInfo.level >= 30) 
            {
                GuildWarIconView.instance.showGuildWarEffect(true);
            }
            else 
            {
                GuildWarIconView.instance.showGuildWarEffect(false);
            }
            return;
        }

        /* internal  */updateGuildwarRingLeftTime(): void
        {
            var loc2=0;
            var loc1=ClockManager.instance.nowDate;
            if (GuildWarScoreView.hasInstance() && cache.guildWar.guildWarInfoNow) 
            {
                loc2 = (cache.guildWar.guildWarInfoNow.ringEndDt.time - loc1.time) / 1000;
                GuildWarScoreView.instance.updateLeftTime(loc2);
            }
            return;
        }

        /* internal  */onGuildWarGetScoreList(arg1: DataEvent): void
        {
            GameProxy.guildWar.getGuildWarScoreList();
            return;
        }

        /* internal  */onGuildWarLeave(arg1: DataEvent): void
        {
            GameProxy.copy.leftGroup_async();
            return;
        }

        /* internal  */onGuildWarOpenOtherMap(arg1: DataEvent): void
        {
            GuildWarFlyScene.instance.show();
            return;
        }

        /* internal  */onGuildWarGetTopList(arg1: DataEvent): void
        {
            GameProxy.guildWar.getGuildWarTopList();
            return;
        }

        /* internal  */onShowOpenModule(arg1: DataEvent): void
        {
            if (GuildwarEnterWindow.instance.isHide) 
            {
                GuildwarEnterWindow.instance.show();
            }
            return;
        }

        /* internal  */onShowOrHideStatusModule(arg1: DataEvent): void
        {
            this.guildWarStatusModule.show();
            this.guildWarStatusModule.isShowLeave(true);
            return;
        }

        private /* var */_currentState: EGuildwarState;

        private /* var */_enterSp: Object /* flash.display.Sprite */;

        private /* var */_enterTime: SecTimer;

        private /* var */_guildWarStatusModule: IGuildwarStatusModule;

        private /* var */_isInGuildWarMap: Boolean=false;

        private /* var */_guildWarOpenModule: IGuildWarOpenModule;
    }
